package co.fitstart.mobile.web.controller.course;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.course.ResultReport;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.courseware.assessment.BasicData;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.mobile.service.BasicInformationService;
import co.fitstart.mobile.service.CourseRoundService;
import co.fitstart.mobile.service.ResultReportService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 成果报告
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/rounds/{courseRoundId}/result-report")
public class ResultReportController {
    private static final Logger logger = LoggerFactory.getLogger(ResultReportController.class);
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    @Autowired
    private ResultReportService resultReportService;
    
    @Autowired
    private BasicInformationService basicInformationService;
    
    /**
     * 成果报告展示页
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView load(@Secure User user,
                             @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("course/course-result-report");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
        	List<BwhRecord> bwhRecords = resultReportService.listBwhRecord(courseRound);
            
            BasicData basicData = basicInformationService.loadBasicData(courseRound);
            
            ResultReport resultReport = resultReportService.loadResultReport(courseRound);
            
            modelAndView.addObject("courseRoundId", courseRoundId);
            modelAndView.addObject("weeks", courseRound.getFitnessGroup().getWeeks());
            modelAndView.addObject("bwhRecords", CollectionUtils.constructMapValueFromList(bwhRecords, "week"));
            modelAndView.addObject("basicData", basicData);
            modelAndView.addObject("resultReport", resultReport);
        }
        
        return modelAndView;
    }
    
    /**
     * 总结编辑页
     * @param user
     * @param courseRoundId
     * @return
     */
    @RequestMapping(value = "/edit-summary", method = RequestMethod.GET)
    public ModelAndView editSummary(@Secure User user,
                                    @PathVariable("courseRoundId") Long courseRoundId) {
        ModelAndView modelAndView = new ModelAndView("course/course-summary-edit");
        
        CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
        
        if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
            ResultReport resultReport = resultReportService.loadResultReport(courseRound);
            
            if(resultReport != null) {
                modelAndView.addObject("courseRoundId", courseRoundId);
                modelAndView.addObject("summary", resultReport.getStudentSummary());
                modelAndView.addObject("wordsToCoach", resultReport.getWordsToCoach());
            }
        }
        
        return modelAndView;
    }
    
    /**
     * 编辑总结
     * @param user
     * @param courseRoundId
     * @param summary
     * @return
     */
    @RequestMapping(value = "/edit-summary", method = RequestMethod.PATCH)
    public @ResponseBody String onEditSummary(@Secure User user,
                                              @PathVariable("courseRoundId") Long courseRoundId,
                                              @RequestParam(value="summary", required=false) String summary,
                                              @RequestParam(value="wordsToCoach", required=false) String wordsToCoach) {
        Result result = new Result();
        
        try {
            CourseRound courseRound = courseRoundService.loadCourseRound(courseRoundId);
            
            if(courseRound != null && !courseRound.isDisabled() && courseRound.getStudent().getUser() == user) {
                resultReportService.editSummary(courseRound, summary, wordsToCoach);
            }
            
            result.success().message("修改总结成功");
        } catch (Exception e) {
            logger.error("ResultReportController#onEditSummary() error", e);
            result.fail().message("修改总结失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
